package com.itstudio.portal.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itstudio.portal.entity.Industry;
import com.itstudio.portal.mapper.IndustryMapper;
import com.itstudio.portal.service.IndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Description:
 * @Author: zhaopengbo
 * @Date: 2024/11/25
 */
@Component
public class IndustryUtil {

    @Autowired
    IndustryMapper industryMapper;

    public Map<String, Map<String,String>> parseIndustry(String address) {

        if (address == null || address.isEmpty()) {
            return Collections.emptyMap();
        }

        String[] parts = address.split(">");
        Map<String, Map<String,String>> industryMap = new HashMap<>();

        List<Map<String,String>> list = new ArrayList<>();
        for (int i = 0; i < parts.length; i++) {
            Map<String,String> itemMap = new HashMap<>();
            LambdaQueryWrapper<Industry> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Industry::getCategoryName, parts[i]);
            Industry industry = industryMapper.selectOne(queryWrapper);
            itemMap.put(industry.getCategoryId(), industry.getCategoryName());
            list.add(itemMap);
        }
        if (list.size() >= 1) {
            industryMap.put("first", list.get(0));
        }
        if (list.size() >= 2) {
            industryMap.put("second", list.get(1));
        }
        if (list.size() >= 3) {
            industryMap.put("third", list.get(2));
        }
        return industryMap;
    }

}
